<?php

/**
 * This file is part of the Codizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Codizer Core Team <team@codizer.org>
 * @package     Codizer
 */

abstract class f_template_AbstractTemplate
{
	protected $discriminator = array();
    protected $cacheGroup;
	
	/**
	 * Disable cache
	 *
	 * @return f_template_AbstractTemplate
	 */
	public function disableCache()
	{
		$this->caching = false;
		return $this;
	}
	
	/**
	 * Set the template file
	 *
	 * @param string $templateFile
	 * 
	 * @return f_template_AbstractTemplate
	 */
	public function setTemplate($templateFile)
	{
		if(is_file(realpath($templateFile)))
		{
			$this->templateFile = $templateFile;	
		}
		else
		{
			throw new Exception("Template file not found :".$templateFile);
		}
		return $this;
	}
	
	/**
	 * Return the template full path
	 *
	 * @return string
	 */
	public function getTemplate()
	{
		return $this->templateFile;
	}
		
	/**
	 * Set module template
	 *
	 * @param string $moduleName
	 * @param string $templateName
	 * 
	 * @return f_template_AbstractTemplate
	 */
	public function setModuleTemplate($moduleName, $templateName)
	{
		$path = DS.$moduleName.DS."templates".DS.$templateName;
		
		if(is_file(WEBAPP_MODULES_DIR.$path))
		{
			$file = WEBAPP_MODULES_DIR.$path;
		}
		else
		{
			$file = MODULES_DIR.$path;
		}
		
		$this->setTemplate($file);
		return $this;
	}
	
	/**
	 * Add a cache discriminator
	 *
	 * @param mixed $discriminator
	 * @return f_template_AbstractTemplate
	 */
	public function addCacheDiscriminator($discriminator, $cacheGroup = null)
	{
        if(!is_null($cacheGroup) && is_string($cacheGroup))
        {
            $this->cacheGroup = $cacheGroup;
        }

		if(is_string($discriminator) || is_int($discriminator) || is_float($discriminator))
		{
			$this->discriminator[] = $discriminator;
		}
		if(is_array($discriminator) && count($discriminator) >=1 )
		{
			$this->discriminator[] = join("-", $discriminator);
		}
		if(is_object($discriminator) && method_exists($discriminator, "getId"))
		{
			$this->discriminator[] = "docId".$discriminator->getUniqueId();
		}
		
		return $this;
	}	
	
	/**
	 * Get discriminator string
	 *
	 * @return string
	 */
	public function getDiscriminator()
	{
        $discriminator = "";

        if(!is_null($this->cacheGroup))
        {
            $discriminator .= $this->cacheGroup;
            if(substr($discriminator, -1, 1) != "|")
            {
                $discriminator .= "|";
            }
        }

        if(count($this->discriminator) > 0)
        {
            $discriminator .= join("-", $this->discriminator);
        }

		return $discriminator;
	}

	/**
	 * Render to the setted Template
	 *
	 * @return string
	 */
	abstract public function render();

    /**
     * Clean up the given template cache file
     */
    abstract public function clearTemplateCache();
	
	/**
	 * Render to the setted Template
	 *
	 * @return string
	 */
	abstract public function renderDisplay();

	/**
	 * build a template render
	 *
	 * @param string $moduleTemplateName
	 * @param string $templateName
	 * @param array $assignments - array("assignVar" => "assignValues")
	 * @return string
	 */	
	abstract public function buildTemplateRender($moduleTemplateName, $templateName, $assignments = array());
}